//
//  SISSCommentShowView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/2.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSCommentShowView.h"
#import "SISSRateBarView.h"
#import "SISSCommentModel.h"

@interface SISSCommentShowView ()

/// titleView
@property (weak, nonatomic) UIView *titleView;
/// titleLabel
@property (weak, nonatomic) UILabel *titleLabel;
/// 时间Label
@property (weak, nonatomic) UILabel *timeLabel;
/// 评分Label
@property (weak, nonatomic) UILabel *gradeLabel;
/// 评分StarView
@property (weak, nonatomic) SISSRateBarView *gradeStarView;
/// 评语Label
@property (weak, nonatomic) UILabel *commentLabel;
/// lineView
@property (weak, nonatomic) UIView *lineView;
/// 追加时间Label
@property (weak, nonatomic) UILabel *addTimeLabel;
/// 追加内容Label
@property (weak, nonatomic) UILabel *addContentLabel;


@end
@implementation SISSCommentShowView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initUI];
        [self initConstraint];
    }
    
    return self;
}

- (void)setCommentModel:(SISSCommentModel *)commentModel {
    _commentModel = commentModel;
    
    self.timeLabel.text = [commentModel.insertTime substringToIndex:10];
    self.gradeStarView.rate = commentModel.score;
    self.commentLabel.text = commentModel.content;
    
    WeakSelf
    CGFloat margin = 8.0;
    if (commentModel.lstOfAdditional.count != 0) {
        [self hideAddtionalCommentOrNoWithIsHidden:NO];
        self.addTimeLabel.text = [NSString stringWithFormat:@"%@追加",[commentModel.lstOfAdditional.firstObject.insertTime substringToIndex:10]];
        self.addContentLabel.text = commentModel.lstOfAdditional.firstObject.content;
        
        [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.gradeLabel);
            make.top.equalTo(weakSelf.gradeLabel.mas_bottom).offset(2 * margin);
            make.right.equalTo(weakSelf).offset(-2 * margin);
        }];
        [self.addContentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.addTimeLabel);
            make.top.equalTo(weakSelf.addTimeLabel.mas_bottom).offset(margin);
            make.bottom.equalTo(weakSelf);
        }];

    } else {
        [self hideAddtionalCommentOrNoWithIsHidden:YES];
        self.addTimeLabel.text = @"";
        self.addContentLabel.text =@"";
        if (commentModel) {
            [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(weakSelf.gradeLabel);
                make.top.equalTo(weakSelf.gradeLabel.mas_bottom).offset(2 * margin);
                make.right.equalTo(weakSelf).offset(-2 * margin);
                make.bottom.equalTo(weakSelf);
            }];
        }
    }
}

- (void)hideAddtionalCommentOrNoWithIsHidden:(BOOL)isHidden {
    
    self.lineView.hidden = isHidden;
    self.addTimeLabel.hidden = isHidden;
    self.addContentLabel.hidden = isHidden;
}

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    
    self.titleLabel.text = titleStr;
}




#pragma mark - UI
- (void)initUI {
    
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:titleView];
    self.titleView = titleView;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = MAIN_TEXT_COLOR;
    titleLabel.text = @"我对他的评价";
    [self.titleView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = [UIFont systemFontOfSize:16];
    timeLabel.textColor = MAIN_TEXT_COLOR;
    timeLabel.text = @"2017-7-28";
    [self addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *gradeLabel = [[UILabel alloc] init];
    gradeLabel.font = [UIFont systemFontOfSize:16];
    gradeLabel.textColor = MAIN_TEXT_COLOR;
    gradeLabel.text = @"评分：";
    [self addSubview:gradeLabel];
    self.gradeLabel = gradeLabel;
    
    SISSRateBarView *gradeView = [[SISSRateBarView alloc] init];
    gradeView.starTintColor = [UIColor redColor];
    [self addSubview:gradeView];
    self.gradeStarView = gradeView;
    
    UILabel *commentLabel = [[UILabel alloc] init];
    commentLabel.font = [UIFont systemFontOfSize:16];
    commentLabel.textColor = MAIN_TEXT_COLOR;
    commentLabel.numberOfLines = 0;
    commentLabel.text = @"评语：";
    [self addSubview:commentLabel];
    self.commentLabel = commentLabel;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:lineView];
    self.lineView = lineView;
    
    UILabel *addTimeLabel = [[UILabel alloc] init];
    addTimeLabel.font = [UIFont systemFontOfSize:16];
    addTimeLabel.textColor = MAIN_TEXT_COLOR;
    addTimeLabel.text = @"2017-7-28追加";
    [self addSubview:addTimeLabel];
    self.addTimeLabel = addTimeLabel;
    
    UILabel *addContentLabel = [[UILabel alloc] init];
    addContentLabel.font = [UIFont systemFontOfSize:16];
    addContentLabel.textColor = MAIN_TEXT_COLOR;
    addContentLabel.text = @"追加内容";
    addContentLabel.numberOfLines = 0;
    [self addSubview:addContentLabel];
    self.addContentLabel = addContentLabel;
}

#pragma mark - 约束
- (void)initConstraint {
    
    CGFloat margin = 8.0;
    WeakSelf
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf).offset(margin);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleView).offset(2 * margin);
        make.centerY.equalTo(weakSelf.titleView);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(2 * margin);
        make.top.equalTo(weakSelf.titleView.mas_bottom).offset(margin);
    }];
    
    [self.gradeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.timeLabel.mas_bottom).offset(margin);
        make.left.equalTo(weakSelf.timeLabel);
    }];
    
    [self.gradeStarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.gradeLabel.mas_right).offset(margin);
        make.centerY.equalTo(weakSelf.gradeLabel);
        make.size.mas_equalTo(CGSizeMake(200, 30));
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.gradeLabel);
        make.top.equalTo(weakSelf.gradeLabel.mas_bottom).offset(2 * margin);
        make.right.equalTo(weakSelf).offset(-2 * margin);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(margin);
        make.right.equalTo(weakSelf).offset(-margin);
        make.top.equalTo(weakSelf.commentLabel.mas_bottom).offset(margin);
        make.height.mas_equalTo(1);
    }];
    
    [self.addTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.commentLabel);
        make.top.equalTo(weakSelf.lineView.mas_bottom).offset(margin);
    }];
    
    [self.addContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.addTimeLabel);
        make.top.equalTo(weakSelf.addTimeLabel.mas_bottom).offset(margin);
        make.right.lessThanOrEqualTo(weakSelf).offset(-2 * margin);
        //        make.bottom.equalTo(self);
    }];
}


@end
